/**
 * Created by Samuel on 16/10/19.
 */
var RouterSingleton = (function () {
    //单例方法
    function Singleton(getKo) {
        this.router = null;
        this.ko = getKo;
        this.routers = initRoutes();
    }
    //单例实例
    var instance;
    //返回对象
    return {
        name: 'RouterSingleton',
        getInstance: function (ko) {
            if (instance === undefined) {
                instance = new Singleton(ko);
                // instance.ko = ko;
                instance.router = Router(instance.routers);
                instance.router.init();
            }
            return instance;
        }
    };
})();


var setEventList = function (path) {

    sInputView(path, function (getViewData) {
        if (getViewData==null){
            RouterSingleton.getInstance().ko.viewDetails("")
            return;
        }
        RouterSingleton.getInstance().ko.viewDetails(getViewData)

    })
};

function initRoutes() {



    var function2 = function () {
        setEventList(m.html2);
    }
    var function3 = function () {
        setEventList(m.html3);
    }
    var books = function () {
        setEventList(null);
        console.log("books");
    };
    var viewBook = function (bookId) {
        console.log("viewBook: bookId is populated: " + bookId);
        setEventList("/viewBook");
    };
    var routers = {
        '/2': function2,
        '/3': function3,
        '/books': [books, function() {
            console.log("An inline route handler.");
        }],
        '/books/view/:bookId': viewBook
    };

    return routers;
}

